#include "sdspeed.h"

void SdSpeedTest::debug_edit_clear()
{
	read_src_filesize_data_label->clear();
	read_dest_filesize_data_label->clear();
	read_time_data_label->clear();
	read_speed_data_label->clear();
	write_src_filesize_data_label->clear();
	write_dest_filesize_data_label->clear();
	write_time_data_label->clear();
	write_speed_data_label->clear();

	src_path_edit->clear();
	debug_edit->clear();

	start_bt->setEnabled(false);
	pProgressBar->setValue(0);
	pProgressBar->setFormat(QString("%1%").arg(0));
	
}

void SdSpeedTest::debug_add_text(QString str)
{
	QDateTime current_date_time = QDateTime::currentDateTime();
	QString current_date = current_date_time.toString("[hh:mm:ss]: ");

	current_date.append(str);
	
	debug_edit->append(current_date); 
}

void SdSpeedTest::debug_add_text(const char *str)
{
	QDateTime current_date_time = QDateTime::currentDateTime();
	QString current_date = current_date_time.toString("[hh:mm:ss]: ");

	current_date.append(QString(str));
	
	debug_edit ->append(current_date); 
}

bool SdSpeedTest::check_valid()
{
	struct usb_mtd_info info;
	struct usb_mtd_info local_info;

//	if(usb_sd_path.startsWith("/mnt"))     						//check it's start with "/mnt"
	{
//		if(usb_sd_path.contains("usb") || usb_sd_path.contains("mmc"))	//check it's contains "usb" or "mmc"
		{
			if(!filename_edit->text().isEmpty()  && (!src_path_edit->text().isEmpty()))		//check the label is valid or empty
			{
				WeiqianFunctions::get_free_space(usb_sd_path.toLocal8Bit().data(), &info);
				WeiqianFunctions::get_free_space("/usrfs", &local_info);
				info.total_size /= 1024;
				info.free_size  /= 1024;
				qDebug("usbpath=%s", usb_sd_path.toLocal8Bit().data());
				qDebug("total=%lldM free=%lldM", info.total_size, info.free_size);
				qDebug("info.free_size=%lld request=%lldM", info.total_size, src_file_size/1024/1024);
				
				if(info.total_size > 1024)    //less than 1G we don't think that is a usb or sd card
				{
					if(info.free_size > filesize_edit->text().toLongLong())			//check the usb free space is enough
					{
						if(local_info.free_size > src_file_size/1024)
						{
							qDebug("usb_free=%lld destfilesize=%lld", info.free_size, filesize_edit->text().toLongLong());
							qDebug("local_free=%lld srcfilesize=%lld", local_info.free_size, src_file_size/1024);
							start_bt->setEnabled(true);
							return true;
						}
					}
				}
			}
		}
	}

	qDebug("check_valid false!");


	debug_edit->setTextColor(Qt::red);
	debug_add_text("***错误*** 参数不合法");

//	if(!usb_sd_path.startsWith("/mnt") || !usb_sd_path.contains("usb"))
//		debug_add_text("路径不正确! 必须是U盘或者SD路径 /mnt/usb/... 或者 /mnt/mmc/...");
	if(info.total_size < 1024)
		debug_add_text("目标总容量小于1G,判为非U盘/SD!请选择一个总容量大于1G的路径");

	if(src_path_edit->text().isEmpty())
		debug_add_text("必须打开U盘里的一个源文件!");

	if(filename_edit->text().isEmpty()) 
		debug_add_text("文件名为空,请检查!");

	if(info.free_size < filesize_edit->text().toLongLong())	
	{
		debug_add_text("U盘剩余空间不足:!");
		debug_add_text(QString("需求空间=%1M 但U盘剩余=%2M").arg(filesize_edit->text()).arg(info.free_size));
	}

	if(local_info.free_size < src_file_size/1024)
	{
		qDebug("local is not enough, need=%lld free=%lld", src_file_size/1024, local_info.free_size);
		debug_add_text("本地空间不足:!");
		debug_add_text(QString("需求空间=%1M 但本地剩余=%2M").arg(src_file_size/1024/1024).arg(local_info.free_size/1024));
	}
	
	debug_edit->setTextColor(Qt::black);
	return false;


}


void SdSpeedTest::open_file()
{
	unsigned int filesize_tmp;
	
	QString unit("b");
	QString path_tmp;

	qDebug("open file");

	read_src_filesize_data_label->clear();
	read_dest_filesize_data_label->clear();
	read_time_data_label->clear();
	read_speed_data_label->clear();
	
	src_file_path = QFileDialog::getOpenFileName(this, tr("Open Image"), "/media"); 
	QFileInfo info_src(src_file_path);

	path_tmp = src_file_path;


	src_file_size	= info_src.size();				
	filesize_tmp	= info_src.size(); 
	src_file_name	= info_src.fileName();

	

	usb_sd_path = path_tmp.replace(src_file_name, QString(""));

	qDebug()<<"usb_path:"<<usb_sd_path;
	qDebug()<<"src_file_name:"<<src_file_name;

	debug_add_text(QString("%1 %2").arg("文件路径: ").arg(src_file_path));

	src_path_edit->setText(src_file_path);

	

	
	dest_file_name 	= QString("/usrfs/").append(src_file_name);

	if(filesize_tmp > 1024)
	{
		filesize_tmp /= 1024;
		unit = "kb";
	}
	if(filesize_tmp > 1024)
	{
		filesize_tmp /= 1024;
		unit = "M";
	}
	if(filesize_tmp > 1024)
	{
		filesize_tmp /= 1024;
		unit = "G";
	}

	qDebug("filesize_tmp=%d\n", filesize_tmp);
	QString file_info_tmp;

	file_info_tmp.sprintf("%d", filesize_tmp);
	file_info_tmp.append(unit);
	
	qDebug() << file_info_tmp;
	read_src_filesize_data_label->setText(file_info_tmp);
	
	debug_add_text(QString("%1 %2 %3 %4字节 %5 %6").arg("文件名:").arg(info_src.fileName()).arg("文件大小:").arg(info_src.size()).arg("约").arg(file_info_tmp));
	qDebug() << QString("%1 %2 %3 %4字节 %5 %6").arg("文件名:").arg(info_src.fileName()).arg("文件大小:").arg(info_src.size()).arg("约").arg(file_info_tmp);

	check_valid();
	


}
void SdSpeedTest::dialog_close()
{
	remove_test_file();

	this->close();
}

#if 0
/*   this is use command "cp", sometime this is  incorrect because of sync   */
void SdSpeedTest::sd_usb_read_test(void)
{	
	long long cur_size;
	debug_add_text("正在进行读测试 ... ");
	

	system(QString("rm -f /usrfs/%1 &").arg(src_file_name).toLocal8Bit().data());
	usleep(2000);
	system("rm -f /tmp/sd_usb_test &");
	usleep(2000);
	system("sync &");
	while(get_file_size("/tmp/sd_usb_test") > 0)
	{
		qDebug("/tmp/sd_usb_test is not removes");
	}

	qDebug("rmcmd = %s\n", QString("rm -f /usrfs/%1 &").arg(src_file_name).toLocal8Bit().data());

	cmd_read = QString("(%1 %2 %3 /usrfs) > /tmp/sd_usb_test 2>&1 &").arg("time").arg("cp").arg(src_file_path); // (time cp /mnt/mmc/mmcblk0p1/user.bin /tmp) > /tmp/sd_usb_test 2>&1 &

	qDebug() << cmd_read;

	system(cmd_read.toLocal8Bit().data());

	qDebug() << dest_file_name;

	qDebug("cur_size=%lld src=%lld\n", get_file_size(dest_file_name.toLocal8Bit().data()), src_file_size);

	while((cur_size = get_file_size(dest_file_name.toLocal8Bit().data())) <= src_file_size)
	{

		if(cur_size > 0)
		{
			qDebug("read cur_size=%lld, src_file_size=%lld percent=%lld", cur_size, src_file_size, cur_size * 100 / src_file_size / 2);
			pProgressBar->setFormat(QString("%1%").arg(cur_size * 100 / src_file_size / 2));
			pProgressBar->setValue(cur_size * 100 / src_file_size / 2);
			
			if(cur_size == src_file_size)
				break;
		}

	}


	system("sync &");

	do
	{
		qDebug("file is not ready: %lld\n", get_file_size("/tmp/sd_usb_test"));
	}while(get_file_size("/tmp/sd_usb_test") == 0);
	

	QFile file("/tmp/sd_usb_test");

    if(!file.open(QFile::ReadOnly))
    {
    	qDebug("open error");	
    	return ;
    }

    
	QTextStream txtInput(&file);  
	QString str;  

	while(!txtInput.atEnd())  
	{  
		str = txtInput.readLine();	
		qDebug() << str;

		if(str.contains("real"))
		{
			read_time = get_time_time(str.toLocal8Bit().data());
			break;
		}
	}
	
	file.close(); 

	qDebug("read_time = %f\n", read_time);

	read_speed = src_file_size/read_time;

	

	if(cur_size == src_file_size)
		read_dest_filesize_data_label->setText(read_src_filesize_data_label->text());

	read_time_data_label->setText(QString::number(read_time, 'f', 2).append("s"));
	read_speed_data_label->setText(QString::number(read_speed/1024/1024, 'f', 2).append("M/s"));
	
	debug_add_text(QString("实际读到大小=%1byte 读取时间=%2s 读取速度=%3byte/s 约%4M/s").arg(cur_size).arg(read_time).arg(QString::number(read_speed, 'f', 1)).arg(QString::number(read_speed/1024/1024, 'f', 6)));
	debug_add_text("读测试完毕");

	qDebug("over");

	

}
#endif

/* this is use cmd "dd", dd if=/mnt/usb/usb-sdb4/m.mp4 of=/usrfs/m.mp4 bs=2M conv=fsync */
void SdSpeedTest::sd_usb_read_test(void)
{	
	long long cur_size;
	debug_add_text("正在进行读测试 ... ");
	

	system(QString("rm -f /usrfs/%1 &").arg(src_file_name).toLocal8Bit().data());
	usleep(2000);
	system("rm -f /tmp/sd_usb_test &");
	usleep(2000);
	system("sync &");
	while(get_file_size("/tmp/sd_usb_test") > 0)
	{
	//	qDebug("read /tmp/sd_usb_test is not removes");
	}

	qDebug("rmcmd = %s\n", QString("rm -f /usrfs/%1 &").arg(src_file_name).toLocal8Bit().data());

    /*                  (time dd if=/mnt/usb/usb-sdb4/m.mp4 of=/usrfs/m.mp4 bs=2M conv=fsync) > /tmp/sd_usb_test 2>&1 & */
	cmd_read = QString("(time dd if=%1 of=/usrfs/%2 bs=20M conv=fsync) > /tmp/sd_usb_test 2>&1 &").arg(src_file_path).arg(src_file_name); 

	qDebug() << cmd_read;

	system(cmd_read.toLocal8Bit().data());
	qDebug() << dest_file_name;

	qDebug("cur_size=%lld src=%lld\n", get_file_size(dest_file_name.toLocal8Bit().data()), src_file_size);

	while((cur_size = get_file_size(dest_file_name.toLocal8Bit().data())) <= src_file_size)
	{
		if(cur_size > 0)
		{
//			qDebug("read cur_size=%lld, src_file_size=%lld percent=%lld", cur_size, src_file_size, cur_size * 100 / src_file_size / 2);
			pProgressBar->setFormat(QString("%1%").arg(cur_size * 100 / src_file_size / 2));
			pProgressBar->setValue(cur_size * 100 / src_file_size / 2);
			
			if(cur_size == src_file_size)
				break;
		}

	}


	system("sync &");

	do
	{
	//	qDebug("file is not ready: %lld\n", get_file_size("/tmp/sd_usb_test"));
	}while(get_file_size("/tmp/sd_usb_test") == 0);
	

	QFile file("/tmp/sd_usb_test");

    if(!file.open(QFile::ReadOnly))
    {
    	qDebug("open error");	
    	return ;
    }

    
	QTextStream txtInput(&file);  
	QString str;  

	while(1)  
	{  
		str = txtInput.readLine();	
		qDebug() << str;

		if(str.contains("real"))
		{
			read_time = get_time_time(str.toLocal8Bit().data());
			break;
		}
	}
	
	file.close(); 

	qDebug("read_time = %f\n", read_time);

	read_speed = src_file_size/read_time;

	

	if(cur_size == src_file_size)
		read_dest_filesize_data_label->setText(read_src_filesize_data_label->text());

	read_time_data_label->setText(QString::number(read_time, 'f', 2).append("s"));
	read_speed_data_label->setText(QString::number(read_speed/1024/1024, 'f', 2).append("M/s"));
	
	debug_add_text(QString("实际读到大小=%1byte 读取时间=%2s 读取速度=%3byte/s 约%4M/s").arg(cur_size).arg(read_time).arg(QString::number(read_speed, 'f', 1)).arg(QString::number(read_speed/1024/1024, 'f', 6)));
	debug_add_text("读测试完毕");

	qDebug("over");

	

}


#if 0

/* this's use cmd "cp",  sometime this is  incorrect because of sync */
void SdSpeedTest::sd_usb_write_test()
{
	debug_add_text("正在进行写测试...");
	long long cur_size;
	QString cmd_creat_file;

	dest_file_size = src_file_size;

	dest_file_path = QString("%1/%2").arg(usb_sd_path).arg(filename_edit->text());

	cmd_creat_file = QString("(time cp /usrfs/%1 %2 &) > /tmp/sd_usb_test 2>&1 &").arg(src_file_name).arg(dest_file_path);
	qDebug("write_cmd=%s", cmd_creat_file.toLocal8Bit().data());


	system(QString("rm -f %1 &").arg(dest_file_path).toLocal8Bit().data());
	qDebug("write_cmd=%s", QString("rm -f %1 &").arg(dest_file_path).toLocal8Bit().data());
	usleep(2000);
	system("rm -f /tmp/sd_usb_test &");
	usleep(2000);
	system("sync &");

	write_src_filesize_data_label->setText(QString("%1M").arg(src_file_size/1024/1024));
	
	while(get_file_size("/tmp/sd_usb_test") > 0)
	{
		qDebug("/tmp/sd_usb_test is not removes");
	}

	system(cmd_creat_file.toLocal8Bit().data());

	while((cur_size = get_file_size(dest_file_path.toLocal8Bit().data())) <= dest_file_size)
	{

		if(cur_size > 0)
		{
			qDebug("write cur_size=%lld, dest_file_size=%lld percent=%lld now=%lld", cur_size,	dest_file_size,  (cur_size * 100 / dest_file_size / 2),  (cur_size*100 / dest_file_size / 2) + 50);
			pProgressBar->setFormat(QString("%1%").arg((cur_size*100 / dest_file_size / 2) + 50));
			pProgressBar->setValue((cur_size*100 / dest_file_size / 2) + 50);

			sleep(2);
			
			if(cur_size == dest_file_size)
			{
				write_dest_filesize_data_label->setText(QString("%1M").arg(dest_file_size/1024/1024));
			
				break;
			}
		}

	}

	
	do
	{
		qDebug("file is not ready: %lld\n", get_file_size("/tmp/sd_usb_test"));
	}while(get_file_size("/tmp/sd_usb_test") <= 0);

	/* open data file */
	QFile file("/tmp/sd_usb_test");

	if(!file.open(QFile::ReadOnly))
	{
		qDebug("open error");	
		return ;
	}

	/* read the /tmp/sd_usb_test and get time */
	QTextStream txtInput(&file);  
	QString str;  

	while(!txtInput.atEnd())  
	{  
		str = txtInput.readLine();	
		qDebug() << str;

		if(str.contains("real"))
		{
			qDebug() << "gettime=" << str;
			write_time = get_time_time(str.toLocal8Bit().data());
			break;
		}
	}
	
	file.close(); 

	qDebug("write_time = %f\n", write_time);

	write_speed = dest_file_size/1024/1024 / write_time;


	write_time_data_label->setText(QString::number(write_time, 'f', 2).append("s"));
	write_speed_data_label->setText(QString::number(write_speed, 'f', 2).append("M/s"));
		
	debug_add_text(QString("实际写入大小=%1byte 写入时间=%2s 写入速度=%3M/s").arg(cur_size).arg(write_time).arg(QString::number(write_speed, 'f', 1)));
	debug_add_text("读测试完毕");

	qDebug("write test over");

}

#endif

#if 1
/* this's use cmd "dd", time dd if=/dev/zero of=/mnt/usb/usb-sdb4/WQ_test_file.zip bs=200M count=1 conv=fsync*/
void SdSpeedTest::sd_usb_write_test()
{
	debug_add_text("正在进行写测试...");
	long long cur_size;
	QString cmd_creat_file;

	dest_file_size = filesize_edit->text().toLongLong()*1024*1024;
	dest_file_path = QString("%1%2").arg(usb_sd_path).arg(filename_edit->text());

	write_src_filesize_data_label->setText(QString("%1M").arg(filesize_edit->text()));

	system(QString("rm -f %1 &").arg(dest_file_path).toLocal8Bit().data());
	qDebug("write_cmd=%s", QString("rm -f %1 &").arg(dest_file_path).toLocal8Bit().data());

	while(get_file_size(dest_file_path.toLocal8Bit().data()) > 0)
	{
	//	qDebug("dest file is not removes");
	}
#if 0
	usleep(2000);
	system("rm -f /tmp/sd_usb_test &");
	usleep(2000);
	system("sync &");


	while(get_file_size("/tmp/sd_usb_test") > 0)
	{
		qDebug("write /tmp/sd_usb_test is not removes");
	}
#endif

	/* touch file in usb */
	cmd_creat_file = QString("(time dd if=/dev/zero of=%1 bs=%2M count=1 conv=fsync) > /tmp/sd_usb_test 2>&1 &").arg(dest_file_path).arg(filesize_edit->text());   

	/* start copy */
	system(cmd_creat_file.toLocal8Bit().data());

	while((cur_size = get_file_size(dest_file_path.toLocal8Bit().data())) <= dest_file_size)
	{

		if(cur_size > 0)
		{
	//		qDebug("write cur_size=%lld, dest_file_size=%lld percent=%lld now=%lld", cur_size,  dest_file_size,  (cur_size * 100 / dest_file_size / 2),  (cur_size*100 / dest_file_size / 2) + 50);
			pProgressBar->setFormat(QString("%1%").arg((cur_size*100 / dest_file_size / 2) + 50));
			pProgressBar->setValue((cur_size*100 / dest_file_size / 2) + 50);
			
			if(cur_size == dest_file_size)
			{
				write_dest_filesize_data_label->setText(filesize_edit->text().append("M"));
			
				break;
			}
		}

	}

	system("sync &");
	debug_add_text("正在获取信息...");


	do
	{
//		qDebug("file is not ready: %lld\n", get_file_size("/tmp/sd_usb_test"));
	}while(get_file_size("/tmp/sd_usb_test") <= 0);

	/* open data file */
	QFile file("/tmp/sd_usb_test");

    if(!file.open(QFile::ReadOnly))
    {
    	qDebug("open error");	
    	return ;
    }

	/* read the /tmp/sd_usb_test and get time */
	QTextStream txtInput(&file);  
	QString str;  

	while(1)  
	{  
		str = txtInput.readLine();	
		qDebug() << str;

		if(str.contains("real"))
		{
			qDebug() << "gettime=" << str;
			write_time = get_time_time(str.toLocal8Bit().data());
			break;
		}
	}
	
	file.close(); 

	qDebug("write_time = %f\n", write_time);

	write_speed = (filesize_edit->text().toInt()) / write_time;


	write_time_data_label->setText(QString::number(write_time, 'f', 2).append("s"));
	write_speed_data_label->setText(QString::number(write_speed, 'f', 2).append("M/s"));
		
	debug_add_text(QString("实际写入大小=%1byte 写入时间=%2s 写入速度=%3M/s").arg(cur_size).arg(write_time).arg(QString::number(write_speed, 'f', 1)));
	debug_add_text("读测试完毕");

	qDebug("write test over");

	/* exit */


}
#endif

void SdSpeedTest::start_sd_usb_test()
{
	debug_add_text("开始测试 !");

	start_bt->setEnabled(false);
	exit_bt->setEnabled(false);
	read_bt->setEnabled(false);
	clear_bt->setEnabled(false);
	remove_bt->setEnabled(false);
	filename_edit->setReadOnly(true);
	filesize_edit->setReadOnly(true);

	read_dest_filesize_data_label->clear();
	read_time_data_label->clear();
	read_speed_data_label->clear();
	write_src_filesize_data_label->clear();
	write_dest_filesize_data_label->clear();
	write_time_data_label->clear();
	write_speed_data_label->clear();

	if(check_valid())
	{	
		sd_usb_read_test();

		sd_usb_write_test();
	}

	start_bt->setEnabled(true);
	exit_bt->setEnabled(true);
	read_bt->setEnabled(true);
	clear_bt->setEnabled(true);
	remove_bt->setEnabled(true);
	filename_edit->setReadOnly(false);
	filesize_edit->setReadOnly(false);
	

}

void SdSpeedTest::remove_test_file(void)
{
	qDebug("dest=%s\n", dest_file_path.toLocal8Bit().data());
	qDebug("tmp=%s\n",  QString("rm -f /usrfs/%1 &").arg(src_file_name).toLocal8Bit().data());


	if(!dest_file_path.isEmpty())   //if file is null
	{
		system(QString("rm -f %1 &").arg(dest_file_path).toLocal8Bit().data());
		debug_add_text(QString("删除 %1 ...").arg(dest_file_path));
		dest_file_path.clear();
	}

	while(get_file_size(dest_file_path.toLocal8Bit().data()) >= 0)
	{
		qDebug("%s is not remove", dest_file_path.toLocal8Bit().data());
	}

	if(!src_file_name.isEmpty())
	{
		system(QString("rm -f /usrfs/%1 &").arg(src_file_name).toLocal8Bit().data());
		debug_add_text(QString("删除 /usrfs/%1 ...").arg(src_file_name));
		src_file_name.clear();
	}

	
	while(get_file_size(QString("/usrfs/%1 &").arg(src_file_name).toLocal8Bit().data()) >= 0)
	{
		qDebug("%s is not remove", QString("/usrfs/%1 &").arg(src_file_name).toLocal8Bit().data());
	}

	src_path_edit->clear();
	debug_add_text("删除完毕");

}



void SdSpeedTest::createImConnect(QString ObjName)
{
    disconnect(im->keyboard, SIGNAL(setvalue(QString)), filename_edit, SLOT(setText(QString)));
    disconnect(im->keyboard, SIGNAL(setvalue(QString)), filesize_edit, SLOT(setText(QString)));
   

    if(ObjName=="filename_edit")
        connect(im->keyboard,SIGNAL(setvalue(QString)), filename_edit, SLOT(setText(QString)));
    if(ObjName=="filesize_edit")
        connect(im->keyboard,SIGNAL(setvalue(QString)), filesize_edit, SLOT(setText(QString)));
   
}



SdSpeedTest::SdSpeedTest(QWidget *parent) : QDialog(parent)
{
	qDebug("new SdSpeedTest\n");

	psplitter = new QScrollArea(); //一个可滚动区域
	im = new InputMethod;

	QGridLayout     *pMainLayout        	= new QGridLayout;
	QHBoxLayout     *pReadParamLayout       = new QHBoxLayout;
	QVBoxLayout     *pWriteParamLayout_1       = new QVBoxLayout;
	QVBoxLayout     *pWriteParamLayout_2       = new QVBoxLayout;
	QHBoxLayout 	*pWriteParamLayout_4	   = new QHBoxLayout;
	QVBoxLayout 	*pbarLayout					   = new QVBoxLayout;
	QGridLayout     *pReadResultLayout        	= new QGridLayout;
	QGridLayout     *pWriteResultLayout        	= new QGridLayout;
	QVBoxLayout 	*pButtonLayout	   = new QVBoxLayout;

	pProgressBar = new QProgressBar;

	src_path_edit = new QLineEdit ;
	filename_edit = new QLineEdit ;
	filesize_edit = new QLineEdit ;
	debug_edit 	  = new QTextEdit ;

	
	start_bt 	= new QPushButton;
	clear_bt	= new QPushButton;
	remove_bt	= new QPushButton;
	exit_bt		= new QPushButton;
	read_bt		= new QPushButton;

	write_param_grpbox 	= new QGroupBox;
	read_param_grpbox	= new QGroupBox;
	write_result_grpbox	= new QGroupBox;
	read_result_grpbox	= new QGroupBox;
	bar_grpbox			= new QGroupBox;

	src_label						= new QLabel;
	filename_label					= new QLabel;
	filesize_label					= new QLabel;
	
	read_src_filesize_label			= new QLabel;
	read_dest_filesize_label		= new QLabel;
	read_time_label					= new QLabel;
	read_speed_label				= new QLabel;

	read_src_filesize_data_label	= new QLabel;
	read_dest_filesize_data_label	= new QLabel;
	read_time_data_label			= new QLabel;
	read_speed_data_label			= new QLabel;

	write_src_filesize_label		= new QLabel;
	write_dest_filesize_label		= new QLabel;
	write_time_label				= new QLabel;
	write_speed_label				= new QLabel;

	write_src_filesize_data_label	= new QLabel;
	write_dest_filesize_data_label	= new QLabel;
	write_time_data_label			= new QLabel;
	write_speed_data_label			= new QLabel;


	src_label->setText("源文件路径");
	filename_label->setText("文件名      ");
	
	filesize_label->setText("文件大小(M) ");	
	
	read_src_filesize_label->setText("源文件大小:");	
	read_dest_filesize_label->setText("实际读大小:");	
	read_time_label 			->setText("耗时  :");	
	read_speed_label			->setText("读速度:");	

#if 0
	read_src_filesize_data_label	->setText("100M");	
	read_dest_filesize_data_label->setText("100M");	
	read_time_data_label			->setText("10s");	
	read_speed_data_label		->setText("5.1M/s");	
#endif
	write_src_filesize_label		->setText("源文件大小:");	
	write_dest_filesize_label		->setText("实际写大小:");	
	write_time_label			->setText("耗时  :");	
	write_speed_label			->setText("写速度:");	
	
#if 0
	write_src_filesize_data_label	->setText("101M");	
	write_dest_filesize_data_label->setText("101M");	
	write_time_data_label			->setText("11s");	
	write_speed_data_label		->setText("6.1M/s");	
#endif	

	start_bt->setText(tr("开始测试"));
	clear_bt->setText(tr("清除信息"));
	remove_bt->setText(tr("删除测试文件"));
	exit_bt	->setText(tr("退出"));
	read_bt	->setText(tr("..."));

	read_bt->setFixedSize(30, 24); 

	start_bt->setEnabled(false);
//	remove_bt->setEnabled(false);

	write_param_grpbox->setTitle(tr("U盘/SD卡写设置"));
	read_param_grpbox->setTitle(tr("U盘/SD卡读设置"));
	write_result_grpbox->setTitle(tr("写测试结果"));
	read_result_grpbox->setTitle(tr("读测试结果"));
	bar_grpbox->setTitle(tr("总进度"));

	src_path_edit->setReadOnly(true);

	src_path_edit->setPlaceholderText("选择U盘里的文件");

	filename_edit->setText("WQ_test_file.zip");
	filesize_edit->setText("200");
	

	pProgressBar->setMinimum(0);  				// 最小值
	pProgressBar->setMaximum(100);  			// 最大值
	pProgressBar->setValue(0);  				// 当前进度
	pProgressBar->setFormat(QString("%1%").arg("0"));
	pProgressBar->setAlignment(Qt::AlignCenter);  // 对齐方式

	pReadParamLayout->addWidget(src_label);
	pReadParamLayout->addWidget(src_path_edit);
	pReadParamLayout->addWidget(read_bt);
	

	
	pWriteParamLayout_1->addWidget(filename_label);
	pWriteParamLayout_1->addWidget(filesize_label);
	pWriteParamLayout_2->addWidget(filename_edit);
	pWriteParamLayout_2->addWidget(filesize_edit);

	pWriteParamLayout_4->addLayout(pWriteParamLayout_1);
	pWriteParamLayout_4->addLayout(pWriteParamLayout_2);


	pReadResultLayout->addWidget(read_src_filesize_label, 		0,0,1,1);
	pReadResultLayout->addWidget(read_dest_filesize_label, 		1,0,1,1);
	pReadResultLayout->addWidget(read_time_label , 				0,3,1,1);
	pReadResultLayout->addWidget(read_speed_label, 				1,3,1,1);
	pReadResultLayout->addWidget(read_src_filesize_data_label,  0,2,1,1);
	pReadResultLayout->addWidget(read_dest_filesize_data_label, 1,2,1,1);
	pReadResultLayout->addWidget(read_time_data_label, 			0,4,1,1);
	pReadResultLayout->addWidget(read_speed_data_label, 		1,4,1,1);

	pWriteResultLayout->addWidget(write_src_filesize_label, 		0,0,1,1);
	pWriteResultLayout->addWidget(write_dest_filesize_label, 		1,0,1,1);
	pWriteResultLayout->addWidget(write_time_label , 				0,3,1,1);
	pWriteResultLayout->addWidget(write_speed_label, 				1,3,1,1);
	pWriteResultLayout->addWidget(write_src_filesize_data_label,  	0,2,1,1);
	pWriteResultLayout->addWidget(write_dest_filesize_data_label, 	1,2,1,1);
	pWriteResultLayout->addWidget(write_time_data_label,			0,4,1,1);
	pWriteResultLayout->addWidget(write_speed_data_label, 			1,4,1,1);

	pbarLayout->addWidget(pProgressBar);

	pButtonLayout->addWidget(start_bt);
	pButtonLayout->addWidget(clear_bt);
	pButtonLayout->addWidget(remove_bt);
	pButtonLayout->addWidget(exit_bt);

	read_param_grpbox->setLayout(pReadParamLayout);
	write_param_grpbox->setLayout(pWriteParamLayout_4);
	read_result_grpbox->setLayout(pReadResultLayout);
	write_result_grpbox->setLayout(pWriteResultLayout);
	bar_grpbox->setLayout(pbarLayout);
	
	debug_edit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

	connect(exit_bt, SIGNAL(clicked()), this, SLOT(dialog_close()));
	connect(read_bt, SIGNAL(clicked()), this, SLOT(open_file()));
	connect(clear_bt, SIGNAL(clicked()), this, SLOT(debug_edit_clear()));
	connect(start_bt, SIGNAL(clicked()), this, SLOT(start_sd_usb_test()));
	connect(remove_bt, SIGNAL(clicked()), this, SLOT(remove_test_file()));

	filename_edit->installEventFilter(im);
    filename_edit->setObjectName("filename_edit");
    filesize_edit->installEventFilter(im);
    filesize_edit->setObjectName("filesize_edit");

    connect(im, SIGNAL(setImObjName(QString)), this, SLOT(createImConnect(QString)));


	pMainLayout->addWidget(read_param_grpbox,		0,0,1,1);
	pMainLayout->addWidget(write_param_grpbox,		0,1,1,1);
	pMainLayout->addWidget(read_result_grpbox,		1,0,1,1);
	pMainLayout->addWidget(write_result_grpbox,		1,1,1,1);
	pMainLayout->addWidget(bar_grpbox,				2,0,1,2);	
	pMainLayout->addWidget(debug_edit,				3,0,1,2);
	pMainLayout->addLayout(pButtonLayout,			1,3,3,1);

	this->setLayout(pMainLayout);
}

SdSpeedTest::~SdSpeedTest()
{
	remove_test_file();
}
